package com.hippo.core.utils

import android.content.Context
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.provider.Settings

object AppUtil {


    fun getAndroidId(context: Context): String {
        return Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
    }


    fun getConnectedWifiName(context: Context): String? {
        val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
        val wifiInfo: WifiInfo? = wifiManager.connectionInfo
        return if (wifiInfo != null && wifiInfo.networkId != -1) {
            wifiInfo.ssid
        } else {
            null
        }
    }

    fun getConnectedWifiSSID(context: Context): String? {
        val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
        val wifiInfo: WifiInfo? = wifiManager.connectionInfo
        return wifiInfo?.ssid?.removeSurrounding("\"")
    }

    fun isBluetoothEnabled(): Boolean {
        val bluetoothAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter()
        return bluetoothAdapter != null && bluetoothAdapter.isEnabled
    }


    fun isValidEmail(email: String): Boolean {
        val emailRegex = "^[A-Za-z0-9+_.-]+@(.+)$".toRegex()
        return email.matches(emailRegex)
    }

    fun parseTime(timeString: String): Pair<Int, Int>? {
        // 使用正则表达式分割字符串
        val timePattern = """(\d{1,2}):(\d{2})""".toRegex()
        val matchResult = timePattern.matchEntire(timeString)

        if (matchResult != null) {
            val (hourStr, minuteStr) = matchResult.destructured
            val hour = hourStr.toInt()
            val minute = minuteStr.toInt()
            return Pair(hour, minute)
        } else {
            // 如果字符串格式不正确，返回 null 或抛出异常
            println("Invalid time format: $timeString")
            return null
        }
    }
}